home *** CD-ROM | disk | FTP | other *** search
Text File | 1993-04-05 | 27.4 KB | 1,115 lines | [TEXT/KAHL] |
- // Source Code for AE101
- // Copyright 1993
- // by Jeffrey B. Kane, MD
- /* put compile time flags here */
-
- /* includes */
- #include <Script.h>
- #include <GestaltEqu.h>
- #include <AppleEvents.h>
- #include <PPCToolBox.h>
-
- /* define */
- #define true 1
- #define false 0
- #define boolean int
- #define SetRect(aRect,l,t,b,r) {(aRect)->top = (t);(aRect)->left = (l);(aRect)->bottom = (b);(aRect)->right = (r);}
-
- /* constants */
- #define kAppleMenuID 128 /* Apple Menu Resource ID */
- #define kFileMenuID 129 /* File Menu Resource ID */
- #define kEditMenuID 130 /* Edit Menu Resource ID */
- #define kSpecialMenuID 131 /* Special Menu Resource ID */
- #define kAboutBoxID 128 /* About box alert Resource ID */
- #define kErrorAlertID 129 /* Error Alert Box */
- #define kWindTemplateID 128 /* template for new windows */
- #define cAboutItem 1
- #define cNewItem 1
- #define cOpenItem 2
- #define cCloseItem 3
- #define cQuitItem 5
- #define cSendQuitItem 1
- #define cSendNewItem 2
- #define cSendCloseItem 3
- #define cSendAboutItem 4
- #define cSendAboutToFinder 6
- #define cSendEmptyTrashToFinder 7
- #define kAENewWindow 'NEW '
- #define kAECloseWindow 'clos'
- #define kAEAbout 'abou'
- #define kAEEmptyTrash 'empt'
- #define kFndrEventClass 'FNDR'
-
- /* prototypes */
- void InitMac(int numMasters);
- void Loop(void);
- void DoMouseDown(EventRecord* theEvent);
- void DoCloseWindow(void);
- void DoNewWindow(void);
- void SetUpMenus(void);
- void DoMenu(EventRecord* theEvent,WindowPtr whichWindow,long selection);
- void doAboutBox(void);
- void DoKey(EventRecord* theEvent);
- void DoUpdate(EventRecord* theEvent);
- void DoOSEvent(EventRecord* theEvent);
- void FixCursor(void);
- extern pascal OSErr doAEQuit(AppleEvent* theAppleEvent, AppleEvent* theReply, long handlerRefcon);
- extern pascal OSErr doAENew(AppleEvent* theAppleEvent, AppleEvent* theReply, long handlerRefcon);
- extern pascal OSErr doAEClose(AppleEvent* theAppleEvent, AppleEvent* theReply, long handlerRefcon);
- extern pascal OSErr doAEAbout(AppleEvent* theAppleEvent, AppleEvent* theReply, long handlerRefcon);
- OSErr MissedRequiredParameters(AppleEvent* theAppleEvent);
- void ConnectToFinder(void);
- void DisconnectFromFinder(void);
- boolean BitTest(long* aValue,int Bit);
- void SetUpperCorner(Rect* theRect,Point aPt);
- void GetUpperCorner(Rect* theRect,Point *aPt);
- void SetLowerCorner(Rect* theRect,Point aPt);
- void GetLowerCorner(Rect* theRect,Point *aPt);
- void SendQuit(void); /* sending an AE Quit */
- void SendNew(void); /* sending an AE New */
- void SendClose(void); /* sending an AE Close */
- void SendAbout(void);
- void SendAboutToFinder(void);
- void SendEmptyTrashToFinder(void);
- void SendEvent(AEEventClass theAEEventClass, AEEventID theAEEventID,AEAddressDesc* theTargetAddressPtr);
- void SendFndrEvent(AEEventID theAEEventID);
- void SendCoreEvent(AEEventID theAEEventID);
- void ErrorAlert(ConstStr255Param theString,OSErr theErr);
- void CopyPStr(char* fromStr,char* toStr);
-
- /* globals */
- boolean gHasColor = false; /* does this machine support 32 bit quickdraw? */
- boolean gProcessor = 0;/* what microprocessor is this? */
- boolean gHasAppleEvents = false;/* do we support IAC (Apple Events)? */
- boolean gFinished = false; /* did the user want to quit? */
- boolean gInForeground = true;/* are we running in the foreground *//* or background? */
- CursHandle gPlusCursor = nil;/* data for the plus cursor */
- boolean gHasPPCToolbox = false;
- RgnHandle gMainContentRgn = nil;
- RgnHandle gAllElseRgn = nil;
- RgnHandle gMouseMovedRgn = nil;
-
- ProcessSerialNumber gFinderPSN = {0L,0L};
- TargetID gFinderTargetID;
-
- MenuHandle AppleMenu = nil;
- MenuHandle FileMenu = nil;
- MenuHandle SpecialMenu = nil;
- MenuHandle EditMenu = nil;
-
- WindowPtr gWindow = nil;
- WindowRecord gWStorage;
-
-
- /* ================================= */
- boolean BitTest(long* aValue,int Bit)
- {
- asm {
- clr.l D0
- clr.l D2
- move.l aValue,A0
- move.l (A0),D1
- move.w Bit,D2
- btst.l D2,D1
- beq.s @1
- moveq.l #1,D0
- @1
- }
-
- }
- /* actual code */
-
- /* ================================= */
- void CopyPStr(char* fromStr, char* toStr)
- {
- short i = 0;
-
- for (i=0; i <= (fromStr[0]); i++)
- {toStr[i] = fromStr[i];}
- } /* CopyPStr */
-
- /* ================================= */
- void ConnectToFinder(void)
- {
- OSErr theErr;
- PortInfoRec thePortInfo;
- LocationNameRec theLocationNameRec;
-
- /* find out who's available *//* (kinda like dating) */
- thePortInfo.authRequired = false;
- thePortInfo.name.nameScript = smRoman; /* english */
- thePortInfo.name.portKindSelector = ppcByString;
- CopyPStr((char*)"\pOscar",(char*)thePortInfo.name.name);
-
- theLocationNameRec.locationKindSelector = ppcNoLocation; /* local machine */
- // since we just stated we are on our // own machine, we don't need to fill //anything else out
-
- theErr = PPCBrowser((ConstStr255Param)"\pLocate the Finder and click on it",
- nil,
- false, /* we are not Specifing a default to find */
- &theLocationNameRec,
- &thePortInfo,
- nil,
- nil);
- /* use the default */
-
- /* construct a targetID from the data provided */
- gFinderTargetID.sessionID = 0; /* we haven't opened a session yet */
-
- /* the PPCBrowser's PPCPortRec *//* Returned */
- BlockMove(&(thePortInfo.name),&(gFinderTargetID.name),sizeof(PPCPortRec));
- gFinderTargetID.location = theLocationNameRec; /* the PPCBrowser's *//* LocationNameRec returned */
- // the system will fill in the recvrName
- // and sesionID after we make a connection
- // we can use them later to speed up
- // AESend
-
- if (theErr != noErr) ErrorAlert((ConstStr255Param)"\pBrowser Result Bad: ",theErr);
- else
- {
- // This is only valid because we are // on a local machine, you have to // use TragetID or SessionIDs if you
- //are connecting out on the network
-
- theErr = GetProcessSerialNumberFromPortName((PPCPortPtr)(&(thePortInfo.name)),
- (ProcessSerialNumberPtr)(&gFinderPSN));
- if (theErr != noErr)
- { ErrorAlert( (ConstStr255Param)"\pError getting Process Serial Number",theErr);
- }
- } /* else */
-
- /* start communications */
-
- } /* ConnectToFinder */
-
- /* ================================= */
- void DisconnectFromFinder(void)
- {
- OSErr theErr;
- PPCClosePBRec thePPCClosePBRec;
- PPCEndPBRec thePPCEndPBRec;
-
- } /* DisconnectFromFinder */
-
-
-
- /* ================================= */
- void InitMac(int numMasters)
- {
- int i;
- long response = 0L;
- OSErr theErr;
- PScrapStuff theScrapStuff;
-
- // set a few enviromental globals that
- // will be handy later
-
- theErr = Gestalt(gestaltQuickdrawFeatures,&response);
- if (BitTest(&response, gestaltHasColor)) gHasColor = true;
-
- theErr = Gestalt( gestaltProcessorType,&response);
- gProcessor = response;
-
- theErr = Gestalt( gestaltAppleEventsAttr, &response);
- if (BitTest(&response, gestaltAppleEventsPresent)) gHasAppleEvents = true;
-
- // see if we need to initalize the PPC
- // toolbox
-
- theErr = Gestalt( gestaltPPCToolboxAttr,&response);
- if (theErr == noErr)
- {
- gHasPPCToolbox = true; // if no error was returned we have a
- // PPC toolbox
- if (BitTest(&response, gestaltPPCSupportsRealTime) != 0)
- theErr = PPCInit(); // if this bit is not set, we need to // intialize
-
- // You can add your own alert in
- //response to the following PPC tests
- if (BitTest(&response, gestaltPPCSupportsOutGoing) != 0);
- // Tell the User to turn on
- // AppleTalk in the Chooser
- if (BitTest(&response, gestaltPPCSupportsIncoming) != 0);
- // Tell the User to activate
- // file sharing or AppleTalk (in
- // the chooser)
-
- }
-
-
- gPlusCursor = GetCursor(plusCursor);
- if (gPlusCursor)
- {
- MoveHHi((Handle)gPlusCursor);
- HLock((Handle)gPlusCursor);
- }
-
- /* now initialize the program itself */
-
- InitGraf(&thePort);
- InitFonts();
- InitMenus();
- InitWindows();
- InitDialogs(nil);
- TEInit();
- InitCursor();
- for (i=1; i > numMasters; i++) MoreMasters();
-
- /* if we have IAC capability, install the AE handler */
- if (gHasAppleEvents)
- {
- theErr = AEInstallEventHandler(kCoreEventClass, kAEQuitApplication,
- (ProcPtr)doAEQuit, 0L, false);
-
- theErr = AEInstallEventHandler(kCoreEventClass, kAENewWindow,
- (ProcPtr)doAENew, 0L, false);
-
- theErr = AEInstallEventHandler(kCoreEventClass, kAECloseWindow,
- (ProcPtr)doAEClose, 0L, false);
-
- theErr = AEInstallEventHandler(kCoreEventClass, kAEAbout,
- (ProcPtr)doAEAbout, 0L, false);
-
- } /* gHasAppleEvents */
-
- /* create the regions to check our mouse against */
- gMainContentRgn = NewRgn();
- gAllElseRgn = NewRgn();
- CopyRgn(GetGrayRgn(),gMainContentRgn);
- gMouseMovedRgn = gAllElseRgn; /* we are initially in an empty desktop */
-
-
- /* Deal with the scrap */
- theScrapStuff = InfoScrap();
- theErr = ZeroScrap();
- {
- char tempScrap[] = "More Stuff";
- PicHandle tempPict = nil;
- Rect tempRect = { -5000,-5000,-4800,-4800 }; /* off screen */
- long scrapSize = 0;
- Ptr thePtr = nil;
- CWindowRecord theWStorage;
- WindowPtr tempWind = nil;
-
- OffsetRect(&tempRect,5050,5050);
- tempWind = NewCWindow(&theWStorage, &tempRect, &"\p", true, plainDBox, (Ptr)(-1), false, 0);
- SetPort(tempWind);
- ClipRect(&(tempWind->portRect));
- ForeColor(greenColor);
- tempRect.left = 0;
- tempRect.top = 0;
- tempRect.right = 100;
- tempRect.bottom = 20;
- tempPict = OpenPicture(&tempRect);
- TextSize(14);
- TextFace(bold | italic);
- TextBox(tempScrap,sizeof(tempScrap),&tempRect,teJustCenter);
- TextFace(0);
- TextSize(12);
- ClosePicture();
- HLock((Handle)tempPict);
- scrapSize = GetHandleSize((Handle)tempPict);
- thePtr = (Ptr)(*tempPict);
- theErr = PutScrap(scrapSize,'PICT',thePtr);
- HUnlock((Handle)tempPict);
- KillPicture(tempPict);
- CloseWindow(tempWind);
- }
- } /* InitMac */
-
- /* ================================= */
- OSErr MissedRequiredParameters(AppleEvent* theAppleEvent)
- {
- /* This is a general use routine to see if a handler missed any */
- /* required parameters */
-
- OSErr theErr;
- Size actualSize;
- DescType returnedType;
-
- /* note that the buffer is nil, and the size we request is zero */
- theErr = AEGetAttributePtr(theAppleEvent,keyMissedKeywordAttr,
- typeWildCard,&returnedType,nil,0,&actualSize);
-
- /* we are ok "if" we don't have any more parameters! */
- if (theErr == errAEDescNotFound) return noErr; /* desired results */
- else if (theErr == noErr) return errAEEventNotHandled; /* more stuff found */
- else return theErr; /* something else happened */
- } /* MissedRequiredParameters */
-
- /* ================================= */
- extern pascal OSErr doAEQuit(AppleEvent* theAppleEvent,AppleEvent* theReply,long handlerRefcon)
- {
- /* Quit Handler for AEs */
- OSErr result = 0;
-
- /* make sure we have all our required parameters */
-
- result = MissedRequiredParameters(theAppleEvent);
- if (result == noErr)
- {
- /* NOTE: if you want to call a routine to double check with the user do it here */
- gFinished = true;
- return result;
- } /* if */
- } /* doAEQuit */
-
-
- /* ================================= */
- extern pascal OSErr doAENew(AppleEvent* theAppleEvent, AppleEvent* theReply, long handlerRefcon)
- {
- /* New window handler for AEs */
- OSErr result = 0;
-
- /* make sure we have all our required parameters */
-
- result = MissedRequiredParameters(theAppleEvent);
- if (result == noErr)
- {
- /* DoStuffHere */
- if (gWindow == nil)
- {
- DoNewWindow();
- }
-
- return result;
- } /* if */
- } /* doAENew */
-
- /* ================================= */
- extern pascal OSErr doAEClose(AppleEvent* theAppleEvent, AppleEvent* theReply, long handlerRefcon)
- {
- /* Close window handler for AEs */
- OSErr result = 0;
-
- /* make sure we have all our required parameters */
-
- result = MissedRequiredParameters(theAppleEvent);
- if (result == noErr)
- {
- /* DoStuffHere */
- if (gWindow != nil)
- DoCloseWindow();
-
- return result;
- } /* if */
- } /* doAEClose */
-
- /* ================================= */
- extern pascal OSErr doAEAbout(AppleEvent* theAppleEvent, AppleEvent* theReply, long handlerRefcon)
- {
- doAboutBox();
- } /* doAEAbout */
-
-
- /* ================================= */
- void DoCloseWindow(void)
- {
- if (gWindow != nil)
- {
- CloseWindow(gWindow);
- gWindow = nil;
- DisableItem(FileMenu,cCloseItem);
- EnableItem(FileMenu,cNewItem);
-
- /* this is only because we are sending from out own application, normally the
- sending app handles this stuff */
- if (gHasAppleEvents)
- {
- DisableItem(SpecialMenu,cSendCloseItem);
- EnableItem(SpecialMenu,cSendNewItem);
- }
-
- FixCursor();
- } /* if */
-
- } /* DoCloseWindow */
-
-
-
- /* ================================= */
- void DoMenu(EventRecord* theEvent,WindowPtr whichWindow,long selection)
- {
- short theMenu;
- short theItem;
- static Str255 theName;
- short trash;
-
- theMenu = HiWord(selection);
- theItem = LoWord(selection);
-
- switch (theMenu) {
- case kAppleMenuID:
- { if (theItem == cAboutItem)
- doAboutBox();
- else {
- GetItem(AppleMenu,theItem,theName);
- trash = OpenDeskAcc(theName);
- }
- break;
- }
- case kFileMenuID:
- switch (theItem) {
- case cNewItem:
- DoNewWindow();
- break;
- case cCloseItem:
- DoCloseWindow();
- break;
- case cQuitItem:
- gFinished = true;
- }
- break;
- case kEditMenuID:
- break;
- case kSpecialMenuID:
- switch (theItem) {
- case cSendQuitItem:
- SendQuit();
- break;
- case cSendNewItem:
- SendNew();
- break;
- case cSendCloseItem:
- SendClose();
- break;
- case cSendAboutItem:
- SendAbout();
- break;
- case cSendAboutToFinder:
- SendAboutToFinder();
- break;
- case cSendEmptyTrashToFinder:
- SendEmptyTrashToFinder();
- break;
- }
- break;
-
- } /* switch */
- HiliteMenu(0);
-
- } /* DoMenu */
-
-
-
-
- /* ================================= */
- void DoMouseDown(EventRecord* theEvent)
- {
- WindowPtr whichWindow;
- long selection;
- Rect dragLimit;
- GDHandle theGDHandle = nil;
- Rect tempRect;
- GrafPtr oldPort = nil;
- Point thePt;
-
- /* calculate a limit for dragging on this mouse click */
- if (gHasColor) {
- theGDHandle = GetGDevice();
- dragLimit = (**theGDHandle).gdRect;
- } /* if */
- else {
- dragLimit = screenBits.bounds;
- } /* else */
-
-
- switch (FindWindow(theEvent->where,&whichWindow)) {
- case inDesk:
- break;
- case inMenuBar:
- selection = MenuSelect(theEvent->where);
- DoMenu(theEvent, whichWindow, selection);
- break;
- case inSysWindow:
- SystemEvent(theEvent);
- break;
- case inContent:
- {
- /* Flash the rectangle, just to show we are there */
- GetPort(&oldPort);
- SetPort(whichWindow);
- tempRect = whichWindow->portRect;
- tempRect.right -= 16;
- tempRect.bottom -= 16;
- thePt = theEvent->where;
- GlobalToLocal(&thePt);
- if (PtInRect(thePt,&tempRect))
- {
- InvertRect(&tempRect);
- InvertRect(&tempRect);
- }
- SetPort(oldPort);
- }
- break;
- case inDrag:
- InsetRect(&dragLimit,4,4);
- dragLimit.top = dragLimit.top + GetMBarHeight();
- DragWindow(whichWindow,theEvent->where,&dragLimit);
- FixCursor();
- break;
- case inGrow:
- {
- tempRect.top = 40;
- tempRect.left = 40;
- tempRect.bottom = (4 + dragLimit.bottom-dragLimit.top);
- tempRect.right = (dragLimit.right-dragLimit.left + 4);
- selection = GrowWindow(whichWindow,theEvent->where,&tempRect);
- SizeWindow(whichWindow,LoWord(selection),HiWord(selection),true);
-
- tempRect = whichWindow->portRect;
- GetPort(&oldPort);
- SetPort(whichWindow);
- InvalRect(&tempRect);
- SetPort(oldPort);
- FixCursor();
- }
- break;
-
- case inGoAway:
- if (TrackGoAway(whichWindow,theEvent->where))
- DoCloseWindow();
- break;
- case inZoomIn:
- if (TrackBox(whichWindow,theEvent->where,inZoomIn))
- {
- GetPort(&oldPort);
- SetPort(whichWindow);
- EraseRect(&(whichWindow->portRect));
- ZoomWindow(whichWindow,inZoomIn,true);
- SetPort(oldPort);
- FixCursor();
- }
- break;
- case inZoomOut:
- if (TrackBox(whichWindow,theEvent->where,inZoomOut))
- {
- GetPort(&oldPort);
- SetPort(whichWindow);
- EraseRect(&(whichWindow->portRect));
- ZoomWindow(whichWindow,inZoomOut,true);
- SetPort(oldPort);
- FixCursor();
- }
- break;
- } /* switch FindWindow */
- } /* DoMouseDown */
-
-
- /* ================================= */
- void DoKey(EventRecord* theEvent)
- {
- char theKey;
- long selection;
-
- /* is the cmd key down? */
- theKey = charCodeMask & theEvent->message;
- if (cmdKey & theEvent->modifiers) {
- selection = MenuKey(theKey);
- DoMenu(theEvent,FrontWindow(),selection);
- } /* if */
- else;
-
-
- } /* DoKey */
-
-
- /* ================================= */
- void DoNewWindow(void)
- {
- Str255 theName = "\p<Untitled>";
-
- gWindow = GetNewWindow(kWindTemplateID,&gWStorage,(WindowPtr)-1L);
- SetWTitle(gWindow,theName);
- ShowWindow(gWindow);
- DisableItem(FileMenu,cNewItem);
- EnableItem(FileMenu,cCloseItem);
- /* this is only because we are sending from out own application, normally the
- sending app handles this stuff */
- if (gHasAppleEvents)
- {
- EnableItem(SpecialMenu,cSendCloseItem);
- DisableItem(SpecialMenu,cSendNewItem);
- }
-
- FixCursor();
-
- } /* DoNewWindow */
-
-
-
- /* ================================= */
- void DoUpdate(EventRecord* theEvent)
- {
- WindowPtr whichWindow;
- GrafPtr oldPort;
- Rect contentRect;
-
- whichWindow = (WindowPtr)theEvent->message;
- GetPort(&oldPort);
- BeginUpdate(whichWindow);
- SetPort(whichWindow);
-
- /* do the real drawing here */
-
- PenSize(3,3);
- contentRect = whichWindow->portRect;
- /* make sure we redraw the scroll bars */
- EraseRect(&contentRect);
- /* now only draw in the real content area */
- contentRect.right -= 18;
- contentRect.bottom -= 18;
- DrawGrowIcon(whichWindow);
- ForeColor(redColor);
- MoveTo(contentRect.left,contentRect.top);
- /* is there a C equiv to "with" ? */
- LineTo(contentRect.right,contentRect.bottom);
- MoveTo(contentRect.right,contentRect.top);
- LineTo(contentRect.left, contentRect.bottom);
-
- /* clean up */
- ForeColor(blackColor);
- PenNormal();
- EndUpdate(whichWindow);
- SetPort(oldPort);
-
- } /* DoUpdate */
-
-
- /* ================================= */
- void DoOSEvent(EventRecord* theEvent)
- {
- int highByte;
-
- /* sign extended shift */
- highByte = theEvent->message >> 24;
- /* mask off the high bits */
- highByte &= 0x00FF;
-
- if (highByte == suspendResumeMessage)
- {
- if (theEvent->message & resumeFlag)
- { /* resuming */
- if (theEvent->message & convertClipboardFlag) {;}
- /* if we wanted the clipboard, we would grab it here */
- else {;}
- /* resume without converting */
-
- /* save time and do your activate stuff from here */
- gInForeground = true;
- }
- else /* suspend event */
- {
- gInForeground = false;
-
- /* convert the clipboard here */
- /* save time and do your deactivate stuff from here */
- }
- } /* suspend/resume event */
- else if (highByte == mouseMovedMessage)
- {
- /* check if we are still over the content area of our window */
- if (PtInRgn(theEvent->where,gMouseMovedRgn))
- {
- /* we are still in the content region */
- ;
- }
- else /* recalculate the region */
- {
- FixCursor();
- }
- } /* mouse moved */
- } /* DoOSEvent */
-
-
- /* ================================= */
- void SetUpperCorner(Rect* theRect,Point aPt)
- {
- theRect->top = aPt.v;
- theRect->left = aPt.h;
-
- } /* SetUpperCorner */
-
-
- /* ================================= */
- void GetUpperCorner(Rect* theRect,Point* aPt)
- {
- aPt->h = theRect->left;
- aPt->v = theRect->top;
-
- } /* GetUpperCorner */
-
-
- /* ================================= */
- void GetLowerCorner(Rect* theRect,Point* aPt)
- {
- aPt->h = theRect->right;
- aPt->v = theRect->bottom;
-
- } /* SetLowerCorner */
-
-
- /* ================================= */
- void SetLowerCorner(Rect* theRect,Point aPt)
- {
- theRect->bottom = aPt.v;
- theRect->right = aPt.h;
-
- } /* SetLowerCorner */
-
-
- /* ================================= */
- void SendCoreEvent(AEEventID theAEEventID)
- {
- ProcessSerialNumber theProcSerialNum;
- AEAddressDesc theTargetAddress;
- OSErr theErr;
-
- /* first create the target... me */
- theProcSerialNum.highLongOfPSN = 0;
- theProcSerialNum.lowLongOfPSN = kCurrentProcess;
- theErr = AECreateDesc(typeProcessSerialNumber, (Ptr)&theProcSerialNum, sizeof(theProcSerialNum),
- &theTargetAddress);
-
- SendEvent(kCoreEventClass,theAEEventID,&theTargetAddress);
- } /* SendCoreEvent */
-
-
- /* ================================= */
- void SendFndrEvent(AEEventID theAEEventID)
- {
- OSType FndrType = 'MACS';
- OSErr theErr;
- AEAddressDesc theTargetAddress;
- Str255 errMsg = "\pProblem creating the target desc";
-
- /* first create the target descriptor... the Finder */
-
- theErr = AECreateDesc(typeProcessSerialNumber, (Ptr)&gFinderPSN, sizeof(gFinderPSN),
- &theTargetAddress);
-
- // #ifdef NOBROWSER
-
- { /* if you wanted to use the FINDER's application signature instead of the PPC browser
- you could use this code instead of the Finder's process serial number */
- OSType theSignature = 'MACS';
-
- theErr = AECreateDesc(typeApplSignature, (Ptr)&FndrType, sizeof(FndrType),
- &theTargetAddress);
- if (theErr) ErrorAlert((ConstStr255Param)&"\pCould not create target addr from Sig",theErr);
- }
-
- { /* if you need to send out info over the network this is how you do it */
- theErr = AECreateDesc(typeTargetID, (Ptr)&gFinderTargetID, sizeof(gFinderTargetID),
- &theTargetAddress);
- }
- // #endif
-
- if (theErr != noErr) ErrorAlert((ConstStr255Param)&errMsg,theErr);
- if (theAEEventID == 'empt')
- SendEvent(kFndrEventClass, theAEEventID, &theTargetAddress);
- if (theAEEventID == 'abou')
- SendEvent(kCoreEventClass, theAEEventID, &theTargetAddress);
-
- }
-
-
- /* ================================= */
- void SendQuit(void)
- {
- SendCoreEvent(kAEQuitApplication);
- } /* SendQuit */
-
-
- /* ================================= */
- void SendNew(void)
- {
- SendCoreEvent(kAENewWindow);
- } /* SendNew */
-
-
- /* ================================= */
- void SendClose(void)
- {
- SendCoreEvent(kAECloseWindow);
- } /* SendClose */
-
-
- /* ================================= */
- void SendAbout(void)
- {
- SendCoreEvent(kAEAbout);
- } /* SendAbout */
-
-
- /* ================================= */
- void SendAboutToFinder(void)
- {
- SendFndrEvent(kAEAbout);
- } /* SendAboutToFinder */
-
-
- /* ================================= */
- void SendEmptyTrashToFinder(void)
- {
- SendFndrEvent(kAEEmptyTrash);
- }
-
-
- /* ================================= */
- void SendEvent(AEEventClass theAEEventClass, AEEventID theAEEventID,
- AEAddressDesc* theTargetAddressPtr)
- {
- OSErr theErr = 0;
- AppleEvent theAppleEvent, theAEReply;
- Str255 ErrMsg = "\pSend Failed, Error #";
-
-
- theErr = AECreateAppleEvent(theAEEventClass,theAEEventID,
- theTargetAddressPtr,kAutoGenerateReturnID,
- kAnyTransactionID,&theAppleEvent);
-
- /* no direct parameters so this one is easy */
-
- /* send it! */
- theErr = AESend(&theAppleEvent,&theAEReply, kAENoReply, kAENormalPriority,
- kAEDefaultTimeout,nil,nil);
-
- /* an IMPORTANT note: we are using kAEDefaultTimeout, but if we chose
- kWaitReply and we are sending to ourselves, we would be waiting a
- long long time (like forever) since we can't reply until we check
- the event loop, and we don't check the event loop until we get a
- reply. Notice a problem? Use a direct send instead */
-
- if (theErr != noErr)
- ErrorAlert((ConstStr255Param)&ErrMsg,theErr);
- } /* SendCoreEvent */
-
-
-
- /* ================================= */
- void ErrorAlert(ConstStr255Param theString,OSErr theErr)
- {
- Str255 *theErrStr, *theErrNumStr;
-
- Str255 stringStorage1, stringStorage2;
-
- theErrStr = &stringStorage1;
- theErrNumStr = &stringStorage2;
-
- theErrStr = &"\p";
- theErrNumStr = &"\p";
-
- switch (theErr)
- {
- case (-609):
- theErrStr = &"\pconnectionInvalid";
- break;
- case (-910):
- theErrStr = &"\pport is already open, maybe in another application";
- break;
- case (-913):
- theErrStr = &"\pPPCPortRec is bad (malformed)";
- break;
- case (-930):
- theErrStr = &"\pbadServiceMethodErr, illegal service type, or not supported";
- break;
- case (-1700):
- theErrStr = &"\perrAECoercionFail";
- break;
- case (-1701):
- theErrStr = &"\perrAEDescNotFound";
- break;
- case (-1702):
- theErrStr = &"\perrAECorruptData";
- break;
- case (-1703):
- theErrStr = &"\perrAEWrongDataType";
- break;
- case (-1704):
- theErrStr = &"\perrAENotAEDesc";
- break;
- case (-1708):
- theErrStr = &"\perrAEEventNotHandled";
- break;
- case (-1709):
- theErrStr = &"\perrAEReplyNotValid";
- break;
- case (-1710):
- theErrStr = &"\perrAEUnknownSendMode";
- break;
- case (-1711):
- theErrStr = &"\perrAEWaitCanceled";
- break;
- case (-1712):
- theErrStr = &"\perrAETimeout";
- break;
- case (-1713):
- theErrStr = &"\perrAENoUserInteraction";
- break;
- case (-1714):
- theErrStr = &"\perrAENotASpecialFunction";
- break;
- case (-1715):
- theErrStr = &"\perrAEParamMissed";
- break;
- case (-1716):
- theErrStr = &"\perrAEUnknownAddressType";
- break;
- case (-1717):
- theErrStr = &"\perrAEHandlerNotFound";
- break;
- case (-1718):
- theErrStr = &"\perrAEReplyNotArrived";
- break;
- case (-1719):
- theErrStr = &"\perrAEIllegalIndex";
- break;
- default:
- NumToString(theErr, theErrNumStr);
- } /* switch */
-
- ParamText(theString,(ConstStr255Param)theErrStr,
- (ConstStr255Param)theErrNumStr,(ConstStr255Param)"\p");
- CautionAlert(kErrorAlertID,nil);
- } /* ErrorAlert */
-
-
-
-
- /* ================================= */
- void FixCursor(void)
- {
- Point where, aPt;
- Rect theRect;
- GrafPtr oldPort;
-
- GetMouse(&where); /* if this is an event you can also use event.where */
- if ((gWindow != nil) && (gInForeground))
- {
- // GetPort(&oldPort);
- // SetPort(gWindow);
-
-
- theRect = (**(((WindowPeek)gWindow)->contRgn)).rgnBBox;
-
- /* adjust for the scroll bars */
- theRect.right -= 16;
- theRect.bottom -= 16;
-
- /* divide the world into two parts, us and them */
- RectRgn(gMainContentRgn, &theRect);
- RectRgn(gAllElseRgn, &(screenBits.bounds));
- UnionRgn(gAllElseRgn,GetGrayRgn(),gAllElseRgn);
- DiffRgn(gAllElseRgn, gMainContentRgn, gAllElseRgn);
-
- if (PtInRect(where,&theRect))
- {
- gMouseMovedRgn = gMainContentRgn;
- SetCursor(*gPlusCursor);
- }
- else
- {
- gMouseMovedRgn = gAllElseRgn;
- InitCursor();
- }
- // SetPort(oldPort);
- }
- else
- { /* there is no winodow */
- InitCursor();
- RectRgn(gAllElseRgn,&(screenBits.bounds));
- UnionRgn(GetGrayRgn(),gAllElseRgn,gAllElseRgn);
- EmptyRgn(gMainContentRgn);
- gMouseMovedRgn = gAllElseRgn;
- }
-
- } /* FixCursor */
-
-
- /* ================================= */
- void Loop(void)
- {
- EventRecord theEvent;
- FixCursor();
-
- do {
- if (WaitNextEvent(everyEvent,&theEvent,15,gMouseMovedRgn))
- {
- switch (theEvent.what) {
- case mouseDown:
- DoMouseDown(&theEvent);
- break;
- case mouseUp:
- break;
- case keyDown:
- case autoKey:
- DoKey(&theEvent);
- break;
- case diskEvt:
- break;
- case activateEvt:
- break;
- case updateEvt:
- DoUpdate(&theEvent);
- break;
- case osEvt:
- DoOSEvent(&theEvent);
- break;
- case kHighLevelEvent:
- if (gHasAppleEvents) AEProcessAppleEvent(&theEvent);
- break;
-
- } /* switch */
- } /* end if WaitNextEvent */
-
-
- } while (!gFinished);
-
-
- } /* Loop */
-
-
- /* ================================= */
- void SetUpMenus(void)
- {
- /* create some menus from the menu resources */
- AppleMenu = GetMenu(kAppleMenuID);
- FileMenu = GetMenu(kFileMenuID);
- EditMenu = GetMenu(kEditMenuID);
- SpecialMenu = GetMenu(kSpecialMenuID);
-
- AddResMenu(AppleMenu,'DRVR');
-
- InsertMenu(AppleMenu,0);
- InsertMenu(FileMenu,0);
- InsertMenu(EditMenu,0);
- if (gHasAppleEvents)
- InsertMenu(SpecialMenu,0);
-
- DrawMenuBar();
-
-
- } /* SetUpMenus */
-
-
- /* ================================= */
- void doAboutBox(void)
- {
- OSErr theErr;
- theErr = Alert(kAboutBoxID,nil);
-
- } /* doAboutBox */
-
-
-
- /* ================================= */
- void main(void)
- {
- InitMac(8);
- SetUpMenus();
- ConnectToFinder();
- Loop();
- DisconnectFromFinder();
- } /* main() */
-
-